﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Word;
using System.IO;


namespace ExamManagementSystem
{
    public partial class ExamPaper : Form
    {
        public ExamPaper()
        {
            InitializeComponent();
        }

        private void ExamPaper_Load(object sender, EventArgs e)
        {
            SqlConnection conn = DBHelper.connection;
            conn.Open();
            string strSql = "select SubjectName from mySubject";
            SqlCommand cmdsubject = new SqlCommand(strSql, conn);
            SqlDataReader dr = cmdsubject.ExecuteReader();
            while (dr.Read())
            {
                this.cbmSubject.Items.Add(dr[0]);
            }
            conn.Close();
        }
        //点击“生成”按钮，生成word文档
        private void btnSheng_Click(object sender, EventArgs e)
        {
            string str = System.Environment.CurrentDirectory;//可执行文件路径
           // str = System.AppDomain.CurrentDomain.BaseDirectory;
            string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//桌面路径
            string nowtime = DateTime.Now.ToString("yyyyMMddhhmmss");


            int xuan = Convert.ToInt32(txtXuan.Text);
            int tian = Convert.ToInt32(txtTian.Text);
            int jian = Convert.ToInt32(txtJian.Text);
            string SubjectName = this.cbmSubject.Text.ToString();
            int maxXuan = CountQuestion(SubjectName, "选择");
            int maxTian = CountQuestion(SubjectName, "填空");
            int maxJian = CountQuestion(SubjectName, "简单");


            //word
            
            string TemplatePath = str + "\\template.dotx";
            Report report = new Report();
            report.CreateNewDocument(TemplatePath);
            report.InsertValue("SubjectName", SubjectName);//添加试卷科目

            report.InsertValue("TeacherName", UserHelper.loginId);//添加出卷人




            ///分别通过存储过程来生成三种题型的试题
            //1.完成选择题选题工作
            for (int i = 0; i < maxXuan; i++)
                QuizzHelper.SelectStates[i] = false;
            Random rand = new Random();
            int RandNumber = 0;
            int Flag = 0;
            for (int temp = 0; temp < xuan; )
            {
                RandNumber = rand.Next(maxXuan - 1);//生成随机数
                if (QuizzHelper.SelectStates[RandNumber] == false)
                {
                    QuizzHelper.SelectStates[RandNumber] = true;
                    temp++;
                }
            }

            //建立连接
            SqlConnection conn = DBHelper.connection;
            conn.Open();
            //建立查询命令
            SqlCommand myCommand = new SqlCommand("proc_exampaperxuan", conn);

            //调用存储过程名
            myCommand.CommandType = CommandType.StoredProcedure;
            //设置存储过程的参数值,其中@id 为存储过程的参数.
            SqlParameter subname = myCommand.Parameters.Add("@subname", SqlDbType.NText);
            subname.Value = this.cbmSubject.Text.ToString();
            //执行命令
            SqlDataReader dr = myCommand.ExecuteReader();//读取数据
            //   int ttt = myCommand.ExecuteNonQuery();
            string s = null;


            int TTT = 1;
            while (dr.Read())
            {
                if (QuizzHelper.SelectStates[Flag] == true)
                {
                    if (s == null)
                        s = TTT + "、" + dr["Question"] + "\nA、" + dr["OptionA"] + "  B、" + dr["OptionB"] + "  C、" + dr["OptionC"] + "  D、" + dr["OptionD"];
                    else
                        s = s + "\n" + TTT + "、" + dr["Question"] + "\nA、" + dr["OptionA"] + "  B、" + dr["OptionB"] + "  C、" + dr["OptionC"] + "  D、" + dr["OptionD"];
                    TTT++;
                }
                Flag++;//题号自加

            }
            conn.Close();
            report.InsertText("Choose", s);//添加选择题

            ///2填空题
            for (int i = 0; i < maxTian; i++)
                QuizzHelper.SelectStates[i] = false;
            Random randT = new Random();
            int RandNumberT = 0;
            int FlagT = 0;
            for (int temp = 0; temp < tian; )
            {
                RandNumberT = randT.Next(maxTian - 1);//生成随机数
                if (QuizzHelper.SelectStates[RandNumberT] == false)
                {
                    QuizzHelper.SelectStates[RandNumberT] = true;
                    temp++;
                }
            }


            conn.Open();
            //建立查询命令
            SqlCommand myCommandT = new SqlCommand("proc_exampapertian", conn);

            //调用存储过程名
            myCommandT.CommandType = CommandType.StoredProcedure;
            //设置存储过程的参数值,其中@id 为存储过程的参数.
            SqlParameter subnameT = myCommandT.Parameters.Add("@subname", SqlDbType.NText);
            subnameT.Value = this.cbmSubject.Text.ToString();
            //执行命令
            SqlDataReader drT = myCommandT.ExecuteReader();//读取数据
            //   int ttt = myCommand.ExecuteNonQuery();
            s = null;

            TTT = 1;
            while (drT.Read())
            {
                if (QuizzHelper.SelectStates[FlagT] == true)
                {
                    if (s == null)
                        s = TTT + "、" + drT["Question"];
                    else
                        s = s + "\n" + TTT + "、" + drT["Question"];
                    TTT++;//题号自加
                }
                FlagT++;

            }
            conn.Close();
            report.InsertText("TianKong", s);//添加填空题


            ///33简答题
            for (int i = 0; i < maxJian; i++)
                QuizzHelper.SelectStates[i] = false;
            Random randJ = new Random();
            int RandNumberJ = 0;
            int FlagJ = 0;
            for (int temp = 0; temp < jian;)
            {
                RandNumberJ = randJ.Next(maxJian - 1);//生成随机数
                if (QuizzHelper.SelectStates[RandNumberJ] == false)
                {
                    temp++;
                    QuizzHelper.SelectStates[RandNumberJ] = true;
                }
            }


            conn.Open();
            //建立查询命令
            SqlCommand myCommandJ = new SqlCommand("proc_exampaperjian", conn);

            //调用存储过程名
            myCommandJ.CommandType = CommandType.StoredProcedure;
            //设置存储过程的参数值,其中为存储过程的参数.
            SqlParameter subnameJ = myCommandJ.Parameters.Add("@subname", SqlDbType.NText);
            subnameJ.Value = this.cbmSubject.Text.ToString();
            //执行命令
            SqlDataReader drJ = myCommandJ.ExecuteReader();//读取数据
            //   int ttt = myCommand.ExecuteNonQuery();
            s = null;
            TTT = 1;
            while (drJ.Read())
            {
                if (QuizzHelper.SelectStates[FlagJ] == true)
                {
                    if (s == null)
                        s = TTT + "、" + drJ["Question"];
                    else
                        s = s + "\n" + TTT + "、" + drJ["Question"];
                    TTT++;//题号自加
                  

                }
                FlagJ++;
            }

                conn.Close();
                report.InsertText("JianDa", s);//添加简答题
                ///三种题型添加完成
                string FileName = desktop + "\\" + UserHelper.loginId +nowtime+ ".docx";
           
                report.SaveDocument(FileName);
                this.Close();
                MessageBox.Show("生成成功");

            
        }


        private void btncancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //获取题目总数
        public int CountQuestion(string SubjectName, string QuestionType)
        {
            //获取SubjectId
            SqlConnection conn = DBHelper.connection;
            conn.Open();
            string strSql = "select * from mySubject where SubjectName='" + SubjectName + "'";
            SqlCommand cmdsubid = new SqlCommand(strSql, conn);
            int isubjectid = Convert.ToInt32(cmdsubid.ExecuteScalar());
            conn.Close();
            //获取题型
            int qt = 0;
            if (QuestionType == "选择")
                qt = 1;
            else if (QuestionType == "填空")
                qt = 2;
            else qt = 3;
            //总数
            conn.Open();
            strSql = "select count(*) from Question,Knowledge where knowledge.KnowledgePointId=Question.KnowledgePoint and QuestionType=" + qt + "and Knowledge.SubjectId=" + isubjectid;
            SqlCommand cmd = new SqlCommand(strSql,conn);
            int Count =Convert.ToInt32(cmd.ExecuteScalar());
            conn.Close();
            return Count;
        }
    }
}